home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / pp / pp-6.0 / Lib / util / diskfull.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-18  |  2.9 KB  |  120 lines

  1. /* diskfull.c: check available space meets requirements */
  2.  
  3. # ifndef lint
  4. static char Rcsid[] = "@(#)$Header: /xtel/pp/pp-beta/Lib/util/RCS/diskfull.c,v 6.0 1991/12/18 20:25:18 jpo Rel $";
  5. # endif
  6.  
  7. /*
  8.  * $Header: /xtel/pp/pp-beta/Lib/util/RCS/diskfull.c,v 6.0 1991/12/18 20:25:18 jpo Rel $
  9.  *
  10.  * $Log: diskfull.c,v $
  11.  * Revision 6.0  1991/12/18  20:25:18  jpo
  12.  * Release 6.0
  13.  *
  14.  */
  15.  
  16. #include "util.h"
  17.  
  18. #if defined(ultrix) && defined(mips)
  19. #include <sys/types.h>
  20. #include <sys/stat.h>
  21. #include <sys/param.h>
  22. #include <sys/mount.h>
  23.  
  24. #define FSTATFS fstatfs
  25. #define STAT_FS    struct fs_data
  26.  
  27. /*
  28. ** Function Name : fstatfs
  29. ** Description   : Get file system statistics.  fstatfs returns
  30. **           information about the filesystem on which an open
  31. **           file resides.  See statfs(3) for more information.
  32. ** Arguments     : fd     - open file descriptor
  33. **                 buffer - ptr to memory to store struct fs_data in
  34. ** Returns       : Upon successful completion, a value of 0 is returned.
  35. **           Otherwise, -1 is returned and the global variable errno
  36. **           is set to indicate the error.
  37. ** Author        : David Bonnell (bonnell@coral.cs.jcu.edu.au)
  38. **               : Department of Computer Science, James Cook University
  39. **         : 7 October, 1991
  40. */
  41. int
  42. fstatfs(int fd, struct fs_data *buffer)
  43. {
  44.   struct stat       buf;
  45.   struct fs_data    mount[NMOUNT];
  46.   int            i, nmount;
  47.  
  48.   if (fstat(fd, &buf) < 0) return -1;        /* get fs dev */
  49.  
  50.   i=0;
  51.   nmount = getmountent(&i, mount, NMOUNT);    /* get sys mount table */
  52.   if (nmount < 0) return -1;
  53.  
  54.   for (i=0; i<nmount; i++) {            /* search for req dev */
  55.     if (mount[i].fd_req.dev == buf.st_dev) {
  56.       *buffer = mount[i];
  57.       return 0;
  58.     }
  59.   }
  60.  
  61.   errno = ENOENT;
  62.   return -1;
  63. }
  64.  
  65. #else
  66. #if defined(BSD42) || defined(masscomp) || defined(HPUX) || defined(_AIX)
  67.     /*BSD type of call */
  68. #define CHECKDISK
  69. #include <sys/vfs.h>
  70. #define STAT_FS    struct statfs
  71. #define FSTATFS fstatfs
  72. #else
  73. #ifdef SYS5            /* sys5 is different (of course!) */
  74. #define CHECKDISK
  75. #include <sys/statvfs.h>
  76. #define STAT_FS struct statvfs
  77. #define FSTATFS fstatvfs
  78. #endif
  79.     /* can't be handled */
  80. #endif
  81. #endif
  82.  
  83.  
  84. fdiskfull (fd, blocks, percent)
  85. int    fd;
  86. int    blocks;
  87. int    percent;
  88. {
  89. #ifdef CHECKDISK
  90.     STAT_FS fs;
  91.  
  92.     if (blocks <= 0 && percent <= 0)
  93.         return OK;
  94.  
  95.     if (FSTATFS (fd, &fs) == NOTOK) {
  96.         PP_SLOG (LLOG_EXCEPTIONS, "failed", ("%s on %d", FSTATFS, fd));
  97.         return OK;    /* and hope... */
  98.     }
  99. #if    defined(ultrix) && defined(mips)
  100.     if (blocks != NOTOK && fs.fd_req.bfree < blocks)
  101. #else
  102.     if (blocks != NOTOK && fs.f_bavail < blocks)
  103. #endif
  104.         return NOTOK;
  105.     if (percent != NOTOK) {
  106.         int tblks;    /* total blocks - reserved */
  107.  
  108. #if    defined(ultrix) && defined(mips)
  109.         tblks = fs.fd_req.btot - (fs.fd_req.bfree - fs.fd_req.bfreen);
  110.         if ((fs.fd_req.bfree * 100 / tblks) < percent)
  111. #else
  112.         tblks = fs.f_blocks - (fs.f_bfree - fs.f_bavail);
  113.         if ((fs.f_bavail * 100 / tblks) < percent)
  114. #endif
  115.             return NOTOK;
  116.     }
  117. #endif
  118.     return OK;        /* if we don't have the facility - lie! */
  119. }
  120.